package com.ffcs.cn.action.desktopcontrol.sqlExpressionDialog;

import com.supermap.data.*;
import com.supermap.desktop.controls.ui.controls.SQLExpressionDialog;
import com.supermap.desktop.controls.ui.controls.SmDialog;
import com.supermap.desktop.controls.ui.controls.comboBox.SmComboBoxDataset;
import com.supermap.desktop.controls.ui.controls.comboBox.SmComboBoxDatasource;
import com.supermap.desktop.core.ui.controls.GridBagConstraintsHelper;
import com.supermap.desktop.core.utilties.DatasetTypeUtilities;
import com.supermap.desktop.core.utilties.FieldTypeUtilities;
import com.supermap.desktop.core.utilties.JOptionPaneUtilities;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**

 */
public class DialogSqlExpression extends SmDialog {
	private JButton buttonShowDialog;

	private JLabel labelSql;
	private JTextField textFieldSql;

	private JLabel labelFieldType;
	private JList<String> listFieldType;

	private JLabel labelWorkspaceTreeSupportDatasetType;
	private JList<String> listWorkspaceTreeSupportDatasetType;

	private JLabel labelDatasource;
	private SmComboBoxDatasource comboBoxDatasource;

	private JLabel labelDataset;
	private SmComboBoxDataset comboBoxDataset;

	private JCheckBox checkBoxShowWorkspaceTree;

	public DialogSqlExpression() {
		setTitle("SQLExpressionDialog 示例");
		setSize(525,490);
		initComponents();
		initValue();
		initLayout();
		initListener();
	}

	private void initListener() {
		checkBoxShowWorkspaceTree.addActionListener(e -> listWorkspaceTreeSupportDatasetType.setEnabled(checkBoxShowWorkspaceTree.isSelected()));
		comboBoxDatasource.addItemListener(e -> {
			Datasource selectedDatasource = comboBoxDatasource.getSelectedDatasource();
			Optional.ofNullable(selectedDatasource).ifPresent(comboBoxDataset::setDatasource);
		});
		//显示 SQLExpressionDialog
		buttonShowDialog.addActionListener(e -> {
			Dataset selectedDataset = comboBoxDataset.getSelectedDataset();
			if (selectedDataset == null){
				JOptionPaneUtilities.showMessageDialog("数据集为空，不支持显示 SQLExpressionDialog ！");
			}
			SQLExpressionDialog expressionDialog;
			if (!checkBoxShowWorkspaceTree.isSelected()){
				expressionDialog = new SQLExpressionDialog();
			}else {
				expressionDialog = new SQLExpressionDialog(true);
			}

			List<String> datasetTypes = this.listWorkspaceTreeSupportDatasetType.getSelectedValuesList();
			if (datasetTypes != null && !datasetTypes.isEmpty()){
				DatasetType[] types = datasetTypes.stream()
						.map(DatasetTypeUtilities::fromString)
						.toArray(DatasetType[]::new);
				expressionDialog.setWorkspaceTreeSupportDatasetType(types);
			}
			List<String> list = listFieldType.getSelectedValuesList();
			List<FieldType> fieldTypes = new ArrayList<>();
			if (list != null && !list.isEmpty()){
				fieldTypes =list.stream()
						.map(FieldTypeUtilities::getFieldType)
						.collect(Collectors.toList());
			}

			String text = textFieldSql.getText();

			if (selectedDataset instanceof DatasetVector){
				expressionDialog.showDialog(fieldTypes,text,((DatasetVector)selectedDataset));
			}
		});
	}

	private void initLayout() {
		this.setLayout(new GridBagLayout());
		this.add(getLeftPanel(), new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP)
				.setWeight(1, 1)
				.setFill(GridBagConstraints.BOTH));

		this.add(getRightPanel(), new GridBagConstraintsHelper(1, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0,
						GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setWeight(1, 1)
				.setFill(GridBagConstraints.BOTH));
	}

	private Component getRightPanel() {
		JPanel panel = new JPanel();
		panel.setLayout(new GridBagLayout());
		panel.setBorder(new TitledBorder("控制面板"));

		panel.add(labelDatasource, new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0)
				.setAnchor(GridBagConstraints.WEST));
		panel.add(comboBoxDatasource, new GridBagConstraintsHelper(1, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST).setWeight(1,0).setFill(GridBagConstraints.HORIZONTAL));

		panel.add(labelDataset, new GridBagConstraintsHelper(0, 1, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0)
				.setAnchor(GridBagConstraints.WEST));
		panel.add(comboBoxDataset, new GridBagConstraintsHelper(1, 1, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.CONTROLS_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST).setWeight(1,0).setFill(GridBagConstraints.HORIZONTAL));

		panel.add(labelSql, new GridBagConstraintsHelper(0, 2, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0)
				.setAnchor(GridBagConstraints.WEST));
		panel.add(textFieldSql, new GridBagConstraintsHelper(1, 2, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.CONTROLS_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST).setWeight(1,0).setFill(GridBagConstraints.HORIZONTAL));

		panel.add(labelFieldType, new GridBagConstraintsHelper(0, 3, 2, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST));
		JScrollPane scrollPane = new JScrollPane(listFieldType);
		scrollPane.setPreferredSize(new Dimension(100,100));
		panel.add(scrollPane, new GridBagConstraintsHelper(0, 4, 2, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST).setWeight(1,1).setFill(GridBagConstraints.BOTH));

		panel.add(checkBoxShowWorkspaceTree, new GridBagConstraintsHelper(0, 5, 2, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST));

		panel.add(labelWorkspaceTreeSupportDatasetType, new GridBagConstraintsHelper(0, 6, 2, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST));
		JScrollPane pane = new JScrollPane(listWorkspaceTreeSupportDatasetType);
		pane.setPreferredSize(new Dimension(100,100));
		panel.add(pane, new GridBagConstraintsHelper(0, 7, 2, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST).setWeight(1,1).setFill(GridBagConstraints.BOTH));
		return panel;
	}

	private Component getLeftPanel() {
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder("控件"));
		panel.add(buttonShowDialog);
		return panel;
	}

	private void initValue() {
		Datasource datasource = comboBoxDatasource.getSelectedDatasource();
		Optional.ofNullable(datasource).ifPresent(comboBoxDataset::setDatasource);

		String[] fieldTypes = Arrays.stream(FieldTypeUtilities.getAllSupportType())
				.map(FieldTypeUtilities::getFieldTypeName)
				.toArray(String[]::new);
		listFieldType.setListData(fieldTypes);

		String[] datasetTypes = Arrays.stream(DatasetTypeUtilities.getDatasetTypeVector())
				.map(DatasetTypeUtilities::toString)
				.toArray(String[]::new);
		listWorkspaceTreeSupportDatasetType.setListData(datasetTypes);
		listWorkspaceTreeSupportDatasetType.setEnabled(checkBoxShowWorkspaceTree.isSelected());
	}

	private void initComponents() {
		buttonShowDialog = new JButton("显示 SQLExpressionDialog");

		labelSql = new JLabel("表达式:");
		textFieldSql = new JTextField();

		labelDatasource = new JLabel("数据源:");
		comboBoxDatasource = new SmComboBoxDatasource();
		comboBoxDatasource.setSupportOpen(true);

		labelDataset = new JLabel("数据集:");
		comboBoxDataset = new SmComboBoxDataset();
		comboBoxDataset.setSupportedDatasetTypes(DatasetTypeUtilities.getDatasetTypeVector());

		labelFieldType = new JLabel("过滤字段类型:");
		listFieldType = new JList<>();

		labelWorkspaceTreeSupportDatasetType = new JLabel("显示工作空间树数据集类型:");
		listWorkspaceTreeSupportDatasetType = new JList<>();

		checkBoxShowWorkspaceTree = new JCheckBox("显示工作空间树");
	}

}
